package com.myk.game.gobangchess.rooms.absrooms;

import com.myk.game.gobangchess.CustomBottom;
import com.myk.game.gobangchess.Player;
import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.constants.WinMode;

/**
 * 各种棋Ai专版的抽象房间
 */
public abstract class AbsAiRoom extends Room {
    public AbsAiRoom(String roomName, String roomIntroduce, String chessName, int[][] curGrids, boolean isSupportPersonal, boolean isSaveHistoryToFile, String... modes) {
        super(roomName, roomIntroduce, chessName, curGrids, isSupportPersonal, isSaveHistoryToFile, modes);
    }

    @Override
    public void setBlackPlayer(Player player) {
        super.setBlackPlayer(player);
        if (player_white == null) {
            setWhitePlayer(createRobotPlayer()); //黑方上座时，白方自动上座机器人
        }
    }

    @Override
    public void setWhitePlayer(Player player) {
        super.setWhitePlayer(player);
        if (player_black == null) {
            setBlackPlayer(createRobotPlayer()); //白方上座时，黑方自动上座机器人
        }
    }

    @Override
    public void playerLeaveSeat(Player player) {
        boolean isOnSeatPlayer = player == player_black || player == player_white;
        super.playerLeaveSeat(player);
        if (isOnSeatPlayer){
            if (player_black != null){
                if (overFlag == 1){
                    doOver(WinMode.WHITE_LEAVE, "白方离开，黑方胜！");
                }
                player_black.leaveRoom();
            }
            else if (player_white != null){
                if (overFlag == 1){
                    doOver(WinMode.BLACK_LEAVE, "黑方离开，白方胜！");
                }
                player_white.leaveRoom();
            }
        }

        /*if (player_black == null && player_white != null) {
            if (player_white instanceof RobotPlayer) {
                player_white.leaveRoom();
            }
            else {
                playerLeaveSeat(player_white);
            }
        }
        else if (player_white == null && player_black != null) {
            if (player_black instanceof RobotPlayer) {
                player_black.leaveRoom();
            }
            else {
                playerLeaveSeat(player_black);
            }
        }*/
    }

    @Override
    public void playerLeaveRoom(Player player) {
        boolean isOnSeatPlayer = player == player_black || player == player_white;
        super.playerLeaveRoom(player);
        if (isOnSeatPlayer){
            if (player_black != null){
                if (overFlag == 1){
                    doOver(WinMode.WHITE_LEAVE, "白方离开，黑方胜！");
                }
                player_black.leaveRoom();
            }
            else if (player_white != null){
                if (overFlag == 1){
                    doOver(WinMode.BLACK_LEAVE, "黑方离开，白方胜！");
                }
                player_white.leaveRoom();
            }
        }

        /*if (player == player_black || player == player_white) {
            if (player_black == null && player_white != null) {
                if (player_white instanceof RobotPlayer) {
                    player_white.leaveRoom();
                }
                else {
                    playerLeaveSeat(player_white);
                }
            }
            else if (player_white == null && player_black != null) {
                if (player_black instanceof RobotPlayer) {
                    player_black.leaveRoom();
                }
                else {
                    playerLeaveSeat(player_black);
                }
            }
        }*/
    }

    @Override
    public void doOver(int winMode, String winMsg) {
        savePlayingInfo(winMode); //记录对弈信息
        saveHistory(winMode); //不同模式存储历史记录

        overFlag = 2; //重置结束状态：对弈结束等待双方确认
        if (winMode == WinMode.BOTH_DRAW) { //双方战平
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            dispatchContinueButton(); //分配重开逻辑
        }
        else if (winMode == WinMode.BLACK_WIN) { //黑方下赢
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            dispatchContinueButton(); //分配重开逻辑
        }
        else if (winMode == WinMode.WHITE_WIN) { //白方下赢
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            dispatchContinueButton(); //分配重开逻辑
        }
        else if (winMode == WinMode.BLACK_ADMIT) { //黑方认输
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            dispatchContinueButton(); //分配重开逻辑
        }
        else if (winMode == WinMode.WHITE_ADMIT) { //白方认输
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            dispatchContinueButton(); //分配重开逻辑
        }
        else if (winMode == WinMode.BLACK_LEAVE) { //黑方逃离
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            //此时不分配重开逻辑
        }
        else if (winMode == WinMode.WHITE_LEAVE) { //白方逃离
            noticeAllWinMsg(winMsg); //通知房间内所有人
            noticeNoMove(); //通知双方都不能再行动了
            //此时不分配重开逻辑
        }
        else if (winMode == WinMode.PERSONAL_RESTART) { //个人对弈模式的认输结束为直接重开
            noticeAllWinMsg(winMsg); //通知房间内所有人
            player_black.removeCustomBottom(player_black.findCustomBottomByTitle(this, "继续"));
            player_black.dispatchCustomBottom();
            reStartDelay(0); //直接重开
        }
        else { //玩家全部离开结束，直接清空界面
            overFlag = 0;//重置结束状态：对弈进行中未结束
            cur = 0;
            resetGrids();//如果黑白都为空，那么对局清空
            noticeAllRefreshGameInfo(); //通知房间内所有人
        }
    }

    protected void sleep(long millis){
        try {
            Thread.sleep(millis);
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    protected void dispatchContinueButton() {
        if (player_black != null && player_white != null) {
            gameTip = "游戏结束！双方点击“继续”可交换位置再来一局。";
            //黑方设置继续按钮
            player_black.removeCustomBottom(player_black.findCustomBottomByTitle(this, "继续"));
            player_black.registerCustomBottom(new CustomBottom(this, "继续", player -> {
                noticeAllBroadcast(player_black.getNickNameOrTemp() + "再来一局");
                player_white.removeCustomBottom(player_white.findCustomBottomByTitle(this, "继续"));
                player_white.dispatchCustomBottom();
                player_black.removeCustomBottom(player_black.findCustomBottomByTitle(this, "继续"));
                player_black.dispatchCustomBottom();
                reStartDelay(0);
            }));
            player_black.dispatchCustomBottom();
            //白方设置继续按钮
            player_white.removeCustomBottom(player_white.findCustomBottomByTitle(this, "继续"));
            player_white.registerCustomBottom(new CustomBottom(this, "继续", player -> {
                noticeAllBroadcast(player_white.getNickNameOrTemp() + "再来一局");
                player_black.removeCustomBottom(player_black.findCustomBottomByTitle(this, "继续"));
                player_black.dispatchCustomBottom();
                player_white.removeCustomBottom(player_white.findCustomBottomByTitle(this, "继续"));
                player_white.dispatchCustomBottom();
                reStartDelay(0);
            }));
            player_white.dispatchCustomBottom();
        }
        else {
            if (player_black != null) {
                player_black.removeCustomBottom(player_black.findCustomBottomByTitle(this, "继续"));
                player_black.dispatchCustomBottom();
            }
            if (player_white != null) {
                player_white.removeCustomBottom(player_white.findCustomBottomByTitle(this, "继续"));
                player_white.dispatchCustomBottom();
            }
        }
    }

    protected abstract Player createRobotPlayer();
}
